
// let base = 'http://202.114.123.116:9999/ai-sensing-back-service/AI_Sensing_Back'
let base = 'http://ai-ecloud.whu.edu.cn/gateway'

 // 全局请求封装
 export default (config) => {
	if (wx.getStorageSync('AI_SENSING_TOKEN')) {
		// config.headers = {'Content-Type': 'application/x-www-form-urlencoded'}
		if(!config.headers){
			config.headers = {}
		}
		// config.headers['Content-Type'] = 'mutipart/form-data;boundary='+new Date().getTime()
	    config.headers['Authorization'] = wx.getStorageSync('AI_SENSING_TOKEN') // 让每个请求携带自定义token 请根据实际情况自行修改
	   
		
	}
	
 	return new Promise((resolve, reject) => {
 		wx.request({
 			url: base + config.url,
 			method: config.method,
			header: config.headers,
 			data: config.data,
 			success(response) {
				switch (response.data.code) {
				    case 200:
						resolve(response.data);
				        return response.data; 
					case 302:
					
				        return response.data;
					case 401:
					  //使用AI_SENSING_REFRESH_TOKEN刷新Token
						wx.removeStorageSync('AI_SENSING_TOKEN');
						if(wx.getStorageSync('AI_SENSING_REFRESH_TOKEN')) {
							const refresh = {
								grant_type: 'refresh_token',
								client_id: 'client-app',
								client_secret: '123456',
								refresh_token: wx.getStorageSync('AI_SENSING_REFRESH_TOKEN')
							};
							
						}
						
						  
						resolve(response.data);
					  break;
				    case 500:
						
						resolve(response.data);
				        break;
				    default:
						resolve(response.data);
						
				}
 			},
 			fail(err) {
 				reject(err);
 			},
 			complete() {
 				wx.hideLoading();
 			}
 		});
 	});
 };


// export default uservice